import Koa from 'koa';
import Router from '@koa/router';

const app = new Koa();
const router = new Router();

// 中间件：日志记录
app.use(async (ctx, next) => {
  console.log(`${ctx.request.method} ${ctx.request.url}`);
  await next();
});

// 路由：首页
router.get('/', async (ctx) => {
  ctx.body = 'Hello, Koa!';
});

// 路由：API 示例
router.get('/api/user', async (ctx) => { 
  ctx.body = { id: 1, name: 'John Doe' };
});
    
// 注册路由
app.use(router.routes()).use(router.allowedMethods());

// 启动服务器
app.listen(9000, () => {
  console.log('Server is running on http://localhost:3000');
});